import Vue from 'vue'
import Router from 'vue-router'
import home from './home'
import category from './category'
import product from './product'
import mine from './user'
import order from './order'
import setting from './setting'

import {getLocalStorage} from '@/utils/local-storage'

Vue.use(Router)

const RouterModel = new Router({
  mode: 'history',
  routes: [
    ...home,
    ...category,
    ...product,
    ...mine,
    ...order,
    ...setting,

  ]
})

RouterModel.beforeEach((to, from, next) => {
  const {Authorization} = getLocalStorage(
    'Authorization'
  )
  console.log(Authorization)
  if (!Authorization) {
    if (to.meta.login) {
      next({name: 'login', query: {redirect: to.name}})
      return
    }
  }
  console.log(to.meta, 'meta')
  // let emptyObj = JSON.stringify(to.meta) === '{}'
  // let undefinedObj = typeof (to.meta.showHeader) === 'undefined'
  // if (!emptyObj && !undefinedObj) {
  //   store.commit('CHANGE_HEADER', to.meta)
  // } else {
  //   store.commit('CHANGE_HEADER', {showHeader: true, title: ''})
  // }
  next()
})
export default RouterModel
